GBML manual / Maths

int_part

The integer part of value, with the fractional part simply dropped - 1.9 becomes 1, and -1.9 becomes -1 (toward zero, never rounded).

Syntax

int_part(value) -> int

Arguments

NameType
valuereal

Returns

int

Description

The integer part of value, with the fractional part simply dropped - 3.75 becomes 3, and -3.75 becomes -3 (toward zero, never rounded up or down to the nearest whole number).

real is a fixed-point number, not a floating-point one, so this is exact - there is no rounding error to worry about the way there would be converting a float.

This is not the same as int_floor, and picking the wrong one is easy to miss until a value goes negative. int_part and int_floor agree for a positive value (int_part(3.75) and int_floor(3.75) are both 3) and disagree for a negative one (int_part(-3.75) is -3, but int_floor(-3.75) is -4). A position just past a room's left or top edge is negative, and so is an offset such as x - 20 near the edge, so a negative value is not rare - if the result means "which grid cell/tile is this," use int_floor, not int_part. int_part is right for the smaller set of cases where "toward zero" is genuinely the rule - a value you know is never negative, or where you specifically want the fractional part discarded rather than the position rounded down.

For "which of several discrete slots is this value closest to" (not "which cell contains it"), see int_round instead.

The part int_part throws away - the 0.75 of 3.75, the -0.75 of -3.75 - is what frac_part keeps. The two always add back up to the number you started with.

Example

var distance_travelled = 27.6; // never negative - only ever grows
var whole_steps = int_part(distance_travelled);

distance_travelled only ever grows, so int_part and int_floor would agree here every time - a case where "toward zero" is a safe, deliberate choice, not a name picked without thinking about the alternative.

On the GBA

real is a 20.12 FIXED-POINT number, not a floating-point approximation - this reads its own stored integer data exactly (the GBA module’s own fixed-point integer function). NOT what grid/tile maths wants once a coordinate can go negative (a position just past a room’s left or top edge is negative, and so is an offset such as x - 20 near the edge, so that is not rare) - int_part(-1.9) is -1, one cell short of where floor would correctly land; see int_floor for that case. Use int_part only where "toward zero" is genuinely the rule you want (e.g. a value that is never negative in practice).

See also: frac_partint_floorint_round